package com.example.bausip;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ExpandableListView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
public class CourseActivity extends Activity implements OnItemSelectedListener {

	ArrayList<Instructor> intsructors = new ArrayList<Instructor>();
	ArrayList<Student> assistants = new ArrayList<Student>();
	Spinner intsructorsSP, assistantsSP;
	//ExpandableListAdapter listAdapter;
	//ExpandableListView expListView;
	List<String> listDataHeader;
	HashMap<String, List<String>> listDataChild;
	int course_id = 0;
	TextView id_txt, name_txt,desc_txt,credits_txt;
	Course mycourse;
	GlobalM glob = new GlobalM();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_course);
		intsructorsSP = (Spinner) findViewById(R.id.instructorSP);
		assistantsSP = (Spinner) findViewById(R.id.assistantSP);
		//populate("instructor");
		//populate("assistant");
		//populateExp();

		Bundle extras = getIntent().getExtras();
		course_id = extras.getInt("course_id");
		Log.d("ray","getting id :"+course_id);
		id_txt = (TextView) findViewById(R.id.courseId);
		id_txt.setFocusable(false) ;
		name_txt = (TextView) findViewById(R.id.courseName);
		desc_txt = (TextView) findViewById(R.id.addDesc);
		credits_txt = (TextView) findViewById(R.id.credits);
		if(!sip.isInstructor())
		{
			disable( (ViewGroup) findViewById(R.id.scroll));
			//disable( (ViewGroup) findViewById(R.id.icons));
		}
		ImageView students = (ImageView) findViewById(R.id.courses);
		if(!sip.isStudent())
		{
			students.setVisibility(View.GONE);
		}
		getCourse(course_id);
	}
	public void disable(ViewGroup main)
	{
		for (int i = 0; i < main.getChildCount(); i++) {
		    View child = main.getChildAt(i);
		    child.setFocusable(false);
		    child.setEnabled(false);
		    child.setClickable(false);
		}
		Button add = (Button) findViewById(R.id.addTask);
		add.setVisibility(View.GONE);
	}
	public void getCourse(int course_id)
	{
		String serverURL = new myURL("read_courses.php?id="+course_id,true).getURL();
		RZHelper p = new RZHelper(serverURL, this, "setCourses", true);
		p.get();
	}
	public void setCourses(String s, String error)
	{
		Log.d("ray","Course info "+s);
		mycourse = new APIManager().getCourses(s).get(0);
		id_txt.setText(mycourse.getId()+"");
		name_txt.setText(mycourse.getCourse_name());
		desc_txt.setText(mycourse.getCourse_description());
		credits_txt.setText(mycourse.getId()+"");
		getInstructors();
		
	}
	public void getInstructors() {
		String serverURL = new myURL("read_instructors.php"/*?id="+department_id*/,true).getURL();
		RZHelper p = new RZHelper(serverURL, this, "setInstructors", true);
		p.get();
	}
	public void setInstructors(String s,String error)
	{
		Log.d("ray", "reply: " + s);
		ArrayList<Instructor> myIs = new APIManager().getInstructors(s);		
		populateInstructors(myIs);
		getAssistants();
	}
	public void getAssistants() {
		String serverURL = new myURL("read_assistants.php"/*?id="+department_id*/,true).getURL();
		RZHelper p = new RZHelper(serverURL, this, "setAssistants", true);
		p.get();
	}
	public void setAssistants(String s,String error)
	{
		Log.d("ray", "reply: " + s);
		ArrayList<Student> myIs = new APIManager().getAssistants(s);		
		populateAssistants(myIs);
	}
	

	public void populateInstructors(ArrayList<Instructor> current ) {
		
		ArrayAdapter<Instructor> instructorAdapter = new ArrayAdapter<Instructor>(this,
				android.R.layout.simple_spinner_item, current);
		instructorAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		instructorAdapter.notifyDataSetChanged();
		
			intsructorsSP.setAdapter(instructorAdapter);
			intsructorsSP.setOnItemSelectedListener(this);
			intsructors = current;
			if (course_id != 0)
				glob.setSelected(intsructorsSP, instructorAdapter, new Instructor(mycourse.getInstructor_id()));
		
	}

	public void populateAssistants(ArrayList<Student> current ) {
		
		ArrayAdapter<Student> assistantAdapter = new ArrayAdapter<Student>(this,
				android.R.layout.simple_spinner_item, current);
		assistantAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		assistantAdapter.notifyDataSetChanged();
		
		assistantsSP.setAdapter(assistantAdapter);
		assistantsSP.setOnItemSelectedListener(this);
		assistants = current;
		
		if (course_id != 0)
			if( mycourse.getAssistant_id()!=0)
				glob.setSelected(assistantsSP, assistantAdapter, new Student( mycourse.getAssistant_id()));
		
	}
	public void goToStudents(View v)
	{
		Intent i = new Intent(getBaseContext(),StudentsActivity.class);
		i.putExtra("course_id", course_id);
		startActivity(i);
	}
	public void goToQuestions(View v)
	{
		Intent i = new Intent(getBaseContext(),QuestionsActivity.class);
		i.putExtra("course_id", course_id);
		startActivity(i);
	}
	public void goToTasks(View v)
	{
		Intent i = new Intent(getBaseContext(),TasksActivity.class);
		i.putExtra("course_id", course_id);
		startActivity(i);
	}
	public void addTask(View v)
	{
		Intent i = new Intent(getBaseContext(),AddTaskActivity.class);
		i.putExtra("course_id", course_id);
		startActivity(i);
	}
	public void showSchedule(View v)
	{
		Intent i = new Intent(getBaseContext(),CourseScheduleActivity.class);
		i.putExtra("course_id", course_id);
		startActivity(i);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.course, menu);
		return true;
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		Log.d("ray", "ray nothing");
	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
		
		Log.d("ray", "selected inst: "+position);
	}

}
